package com.yanqu.road.server.logic.activity.condition.pasture.channel;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.args.ValueParamArgs;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.cross3.activity.EnterNameProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseNewRankActivityCondition;
import com.yanqu.road.server.manger.activity.entername.EnterNameActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.ChannelProtocol;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;

public class ChannelPastureMaxScoreRankCondition extends BaseNewRankActivityCondition {

    int lastUpdateTime = 0;

    public ChannelPastureMaxScoreRankCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        ValueParamArgs args = (ValueParamArgs)object;
        if(args.getValue() > getValue().longValue()){
            setValue(BigInteger.valueOf(args.getValue()));

            //上报单局最高数据
            EnterNameProto.ChannelEnterNameChannelUserValueReqMsg.Builder upload = EnterNameProto.ChannelEnterNameChannelUserValueReqMsg.newBuilder();
            upload.setUserKey(EnterNameActivityMgr.buildKeyMsg(player.getUserId()));
            upload.setActivityId(baseActivityData.getActivityId());
            upload.setType(getConditionType());
            upload.setValue(String.valueOf(args.getValue()));
            upload.setParam(args.getParam());
            int currentSecond = DateHelper.getCurrentSecond();
            if(lastUpdateTime == 0){
                upload.setUserBaseInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
                lastUpdateTime = currentSecond;
            }else {
                if(currentSecond - lastUpdateTime > 30){
                    upload.setUserBaseInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
                    lastUpdateTime = currentSecond;
                }
            }
            player.sendChannelPacket(ChannelProtocol.N_ENTER_NAME_CHANNEL_USER_VALUE, upload);
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.ChannelPastureMaxScoreRank.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.ChannelPastureMaxScoreRank.getValue(), handle);
    }
}
